<?php
namespace app\index\job;
 
use think\App;
use think\facade\Log;
use think\queue\Job;
 
abstract class Queue
{
    /**
     * 创建任务
     */
    public function fire(Job $job, $data)
    {
        if (!$data) {
            Log::error(sprintf(' ==> [%s][%s] 队列无消息', __CLASS__, __FUNCTION__));
        }
        $result = $this->execute($data);
        if ($result) {
            Log::record(sprintf(' ==> [%s][%s] 队列执行成功', __CLASS__, __FUNCTION__));
            $job->delete();
        } else {
            if ($job->attempts() > 3) {
                Log::error(sprintf(' ==> [%s][%s] 队列执行重试次数超过3次，执行失败', __CLASS__, __FUNCTION__));
                $job->delete();
            } else {
                Log::error(sprintf(' ==> [%s][%s] 队列执行失败，延迟3秒后重试', __CLASS__, __FUNCTION__));
                $job->release(3);
            }
        }
    }
    /**
     * 执行业务逻辑
     */
    abstract protected function execute($data): bool;
}
